/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.plugins.impl;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.grayrabbit.cms.dao.plugins.SurveySubjectDao;
import org.grayrabbit.cms.entity.SurveySubject;
import org.grayrabbit.cms.vo.plugins.SurveySubjectBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>SurveySubject</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 16:08:41 
 */
@Repository
public class SurveySubjectDaoImpl extends GenericHibernateDao<SurveySubject, String>
		implements SurveySubjectDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From SurveySubject as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see SurveySubjectDao#assembly(SurveySubjectBean)
	 */
	public QueryCondition assembly(SurveySubjectBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see SurveySubjectDao#assemblyCount(SurveySubjectBean)
	 */
	public QueryCondition assemblyCount(SurveySubjectBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see SurveySubjectDao#count(SurveySubjectBean)
	 */
	public int count(SurveySubjectBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see SurveySubjectDao#assemblyNameQuery(SurveySubjectBean)
	 */
	public QueryCondition assermblyNameQuery(SurveySubjectBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see SurveySubjectDao#assemblyNameQueryCount(SurveySubjectBean)
	 */
	public QueryCondition assemblyNameQueryCount(SurveySubjectBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see SurveySubjectDao#findByPage(Page, SurveySubjectBean)
	 */
	public List<SurveySubject> findByPage(Page<SurveySubject> page,
			SurveySubjectBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(SurveySubjectBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getSurvey().getSurveyId())){
				qc.add(" And obj.survey.surveyId = ?", bean.getSurvey().getSurveyId());
			}
		}
	}
	/**
	 * 根据voteId查询voteOption列表
	 * @param voteId
	 * @return
	 */
	public List<SurveySubject> findBySurveyId(String survetId){
		QueryCondition qc = assembly(null);
		if(StringUtils.isNotBlank(survetId)){}
		qc.add(" And obj.survey.surveyId = ? ", survetId);
		qc.append(" order by obj.ord asc");
		return findByQc(qc);
	}
	/**
	 * 根据id查找记录列表
	 * @param id
	 * @return
	 */
	public List<SurveySubject> findBySubId(String id){
		QueryCondition qc = assembly(null);
		qc.add(" And obj.subjectId = ? ", id);
		return findByQc(qc);
	}
}